import random
from urllib import request
from bs4 import BeautifulSoup
import datetime
import time
import xlwt
from fake_useragent import UserAgent
ua = UserAgent()
row = 0
col = 0
filename = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))

year = int(input("input year:"))
month = int(input("input start month:"))
day = int(input("input start day:"))
end_month = int(input("input end month:"))
end_day = int(input("input end day:"))

begin = datetime.date(year, month, day)
end = datetime.date(year, end_month, end_day)
book = xlwt.Workbook(encoding='utf-8', style_compression=0)
sheet = book.add_sheet('cg_sheet', cell_overwrite_ok=True)

for i in range((end - begin).days + 1):
    day = begin + datetime.timedelta(days=i)
    date = str(day)
    newua = ua.random
    print(date)
    print(newua)
    url = "http://www.okooo.com/jingcai/kaijiang/?LotteryType=SportteryWDL&StartDate=" + date + "&EndDate=" + date
    headers = {'User-Agent': newua}
    page = request.Request(url, headers=headers)
    page_info = request.urlopen(page).read().decode('gb2312')
    # print(page_info)
    soup = BeautifulSoup(page_info, 'html.parser')
    cons = soup.find_all('tr', 'BlackWords')
    file = open(r'caiguo_' + filename + '.txt', 'a')
    for con in cons:
        row = 0
        for tag in con.find_all('td', limit=18):
            tag_con = ',' + tag.text
            file.write(tag_con)
            sheet.write(col, row, tag.text)
            row += 1
        file.write('\n')
        col += 1
    book.save('caiguo_' + filename + '.xls')
    time.sleep(20+random.randint(1,10))
